package cn.lanqiao.active;

import java.util.Random;
import java.util.Scanner;

public class Lucky {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String isContinue;
        String userName = null;
        String password = null;
        int card = -1;
        //User[] users = null;
        boolean isLogin = false;
        do {
            System.out.println("***** 抽奖系统 *****");
            System.out.println("***** 1、注册 *****");
            System.out.println("***** 2、登录 *****");
            System.out.println("***** 3、抽奖 *****");
            System.out.println("请选择：");
            int userSelect = input.nextInt();
            switch (userSelect) {
                case 1:
                    System.out.println("抽奖系统 -> 注册");
                    //用户输入数据
                    System.out.println("请输入用户名：");
                    userName = input.next();
                    System.out.println("请输入密码：");
                    password = input.next();
                    //生成随机卡号
                    card = new Random().nextInt(10);
                    //注册成功，输出用户信息
                    System.out.println("注册成功！");
                    System.out.println("用户名\t密码\t卡号");
                    System.out.format("%s\t%s\t%s\n", userName, password, card);
                    break;
                case 2:
                    System.out.println("抽奖系统 -> 登录");
                    //用户输入数据
                    System.out.println("请输入用户名：");
                    String name = input.next();
                    System.out.println("请输入密码：");
                    String pass = input.next();
                    //用户匹对
                    if (name.equals(userName) && pass.equals(password)) {
                        System.out.println("登录成功！");
                        isLogin = true;
                    } else {
                        System.out.println("登录失败！");
                    }
                    break;
                case 3:
                    System.out.println("抽奖系统 -> 抽奖");
                    int[] cards = new int[5];

                    //判断是否登录
                    if (!isLogin) {
                        System.out.println("请先注册登录！");
                        break;
                    }

                    //生成5个随机数
                    System.out.println("你的卡号是:" + card);
                    System.out.print("幸运数字:");
                    createLuckyCards(cards); //调用方法获得幸运卡号数组
                    System.out.println();
                    //判断是否中奖
                    boolean flag = false;
                    for (int i = 0; i < cards.length; i++) {
                        if (cards[i] == card) {
                            flag = true;
                        }
                    }
                    if (flag) {
                        System.out.println("恭喜中奖！！！");
                    } else {
                        System.out.println("sorry!!!");
                    }
                    break;
                default:
                    System.out.println("输入错误！");
            }
            System.out.println("是否继续？Y/N");
            isContinue = input.next();
        } while ("Y".equalsIgnoreCase(isContinue));

    }

    public static void createLuckyCards(int[] cards) {
        for (int i = 0; i < cards.length; i++) {
            cards[i] = new Random().nextInt(10);
            System.out.print(cards[i] + "\t");
        }
    }


}
